package com.sservice.banfu.service.impl;

import com.sservice.banfu.e.SaleInfo;
import com.sservice.banfu.e.SalePerson;
import com.sservice.banfu.mapper.SaleInfoMapper;
import com.sservice.banfu.mapper.SalePersonMapper;
import com.sservice.banfu.service.SalePersonService1;
import com.sservice.banfu.vo.SalePersonVo;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.base.utils.ObjectUtils;
import com.sservice.generator.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Service
public class SalePersonServiceImpl1 extends AbstractService<SalePerson> implements SalePersonService1 {
    @Autowired
    private SalePersonMapper salePersonMapper;
    @Autowired
    private SaleInfoMapper saleInfoMapper;

    /***
     * 详情
     * @param id
     * @return
     */
    @Override
    public SalePersonVo selectByPrimaryKey(Integer id) {
        SalePerson sp = salePersonMapper.selectByPrimaryKey(id);
        SaleInfo saleInfo = new SaleInfo();
        saleInfo.setSalePersonId(String.valueOf(id));

        SalePersonVo salePersonVo = new SalePersonVo();
//        salePersonVo
        SalePersonVo salePersonVo1 = (SalePersonVo) ObjectUtils.mergeTo(salePersonVo, sp, false);
        SaleInfo si = saleInfoMapper.selectOne(saleInfo);
        if (si == null) return salePersonVo1;
        SalePersonVo salePersonVo2 = (SalePersonVo) ObjectUtils.mergeTo(salePersonVo1, si, false);
        return salePersonVo2;
    }

    @Override
    public SalePerson selectBase(Integer id) {
        return salePersonMapper.selectByPrimaryKey(id);
    }

    @Override
    public SalePerson selectByPhone(SalePerson user) {
        return salePersonMapper.selectOne(user);
    }

    @Override
    public List<SalePerson> queryByPage(SalePerson user) {
        Example example = new Example(SalePerson.class);
        example.createCriteria().andEqualTo("status", user.getStatus())
                .andEqualTo("type1", user.getType1());
        return salePersonMapper.selectByExample(example);
    }

    @Override
    public List<SalePerson> queryByUserIdAndPage(SalePerson user) {
        return salePersonMapper.selectByCondition(user);
    }

    @Override
    public int queryCount(SalePerson user) {
        Example example = new Example(SalePerson.class);
        example.createCriteria().andEqualTo("status", user.getStatus())
                .andEqualTo("type1", user.getType1());
        return salePersonMapper.selectCountByExample(example);
    }

    @Override
    public int querySaleCashCount(SalePerson user) {
        return salePersonMapper.selectCount(user);
    }

    @Override
    public List<SalePerson> querySaleCashByPage(SalePerson user) {
        return salePersonMapper.selectByCondition(user);
    }

    @Override
    public SalePersonVo detail(SalePerson user) {
        SalePerson salePerson1 = findById(user.getId());
        SaleInfo saleInfo = new SaleInfo();
        saleInfo.setSalePersonId(String.valueOf(user.getId()));
        SaleInfo si = saleInfoMapper.selectOne(saleInfo);
        SalePersonVo salePerson2 = new SalePersonVo();
        if (si != null) {
            if (si.getProfile() == null) {
                si.setRemark("");
            } else {
                si.setRemark(new String(si.getProfile()));
            }
            salePerson2.setSi(si);
        }
        salePerson2.setType1(salePerson1.getType1());
        salePerson2.setId(salePerson1.getId());
        salePerson2.setShowNameUserName(salePerson1.getShowNameUserName());
        salePerson2.setSalesCount(salePerson1.getSalesCount());
        salePerson2.setAvatar(salePerson1.getAvatar());
        salePerson2.setCheckStatus(salePerson1.getCheckStatus());
        salePerson2.setEmail(salePerson1.getEmail());
        salePerson2.setFavor(salePerson1.getFavor());
        salePerson2.setName(salePerson1.getName());
        salePerson2.setUserName(salePerson1.getUserName());
        return salePerson2;
    }

    @Override
    public SaleInfo selectByOpenId(String openid) {
        SaleInfo saleInfo = new SaleInfo();
        saleInfo.setOpenid(openid);
        return saleInfoMapper.selectOne(saleInfo);
    }

    @Override
    public List<SalePerson> querySaleManager(SalePersonVo sp) {
        return salePersonMapper.selectByCondition(sp);
    }

    @Override
    public int querySaleManagerCount(SalePerson sp) {
        return salePersonMapper.selectCount(sp);
    }

    @Override
    public List<SalePersonVo> page(SalePersonVo user) {
        List<SalePersonVo> list = salePersonMapper.selectCombine(user);
        List<SalePersonVo> list1 = new ArrayList<SalePersonVo>();
        for (SalePersonVo sp : list) {
            if (sp.getProfile() != null) {
                sp.setRemark(new String(sp.getProfile()));
            }
            list1.add(sp);
        }
        return list1;
    }

    @Override
    public int count(SalePersonVo sp) {
        return salePersonMapper.countCombine(sp);
    }

    @Override
    public ListResult queryTeacher(SalePersonVo sp) {
        ListResult lr = new ListResult(true, salePersonMapper.queryTeacher(sp));
        lr.setPage(new Pagination(sp.getPage(), salePersonMapper.countCombine(sp), sp.getSize()));
        return lr;
    }
}
